<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="序列号" prop="serialNumber">
        <el-input
          v-model="queryParams.serialNumber"
          placeholder="请输入序列号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否可用" prop="availableStatus">
        <el-select v-model="queryParams.availableStatus" placeholder="请选择是否可用" clearable>
          <el-option
            v-for="dict in dict.type.treasure_available"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否出租" prop="rentalStatus">
        <el-select v-model="queryParams.rentalStatus" placeholder="请选择是否出租" clearable>
          <el-option
            v-for="dict in dict.type.treasure_rental"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
<!--      <el-form-item label="站点" prop="stationId">-->
<!--        <el-input-->
<!--          v-model="queryParams.stationId"-->
<!--          placeholder="请输入站点"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['system:treasure:add']"-->
<!--        >租借</el-button>-->
<!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="treasureList" @selection-change="handleSelectionChange">
<!--      <el-table-column type="selection" width="55" align="center" />-->
      <el-table-column label="序号" type="index" width="50" align="center">
        <template slot-scope="scope">
          <span>{{(queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1}}</span>
        </template>
      </el-table-column>
      <el-table-column label="序列号" align="center" prop="serialNumber" />
      <el-table-column label="是否可用" align="center" prop="availableStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.treasure_available" :value="scope.row.availableStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="是否出租" align="center" prop="rentalStatus">
        <template slot-scope="scope">
          <el-tag effect="dark" type="success" v-if="scope.row.rentalStatus==1">是</el-tag>
          <el-tag effect="dark" type="info" v-else>否</el-tag>
<!--          <dict-tag :options="dict.type.treasure_rental" :value="scope.row.rentalStatus"/>-->
        </template>
      </el-table-column>
      <el-table-column label="站点" align="center" prop="stationName" >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleShowMap(scope.row)"
          >{{ scope.row.stationName }}</el-button>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleRent(scope.row)"
            v-hasPermi="['order:treasure:rent']"
          >租借</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修改充电宝对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="序列号" prop="serialNumber">
          <el-input v-model="form.serialNumber" placeholder="请输入序列号" />
        </el-form-item>
        <el-form-item label="是否可用" prop="availableStatus">
          <el-select v-model="form.availableStatus" placeholder="请选择是否可用">
            <el-option
              v-for="dict in dict.type.treasure_available"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否出租" prop="rentalStatus">
          <el-select v-model="form.rentalStatus" placeholder="请选择是否出租">
            <el-option
              v-for="dict in dict.type.treasure_rental"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="站点" prop="stationId">
          <el-input v-model="form.stationId" placeholder="请输入站点" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <rent-promt ref="rentPromt" @close="rentPromtOpen=false" @ok="handleRentOk" />
    <position-map @close="mapOpen=false" ref="positionMap"/>
    <is-rent-dialog ref="isRentDialog" @rent="handleShowRent" />
  </div>
</template>

<script>
import { listTreasure, getTreasure, delTreasure, addTreasure, updateTreasure } from "@/api/system/treasure";
import { rent } from '@/api/system/order'
import rentPromt from "@/views/treasure/components/rentPromt";
import positionMap from "@/views/station/components/positionMap";
import IsRentDialog from "@/views/treasure/components/isRentDialog.vue";


export default {
  name: "Treasure",
  dicts: ['treasure_available', 'treasure_rental'],
  components: {
    rentPromt, positionMap, IsRentDialog
  },
  data() {
    return {
      isRentDialogOpen: true,
      mapOpen: false,
      rentPromtOpen: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 充电宝表格数据
      treasureList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        serialNumber: null,
        availableStatus: null,
        rentalStatus: null,
        stationId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      orderForm: {
        orderId: null,
        orderNumber: null, // 订单号（后台生成）
        userId: null, // 当前登录用户（后台添加）
        orderStatus: null, // 默认1（后台添加）
        paymentMethod: null,
        paymentStatus: null,
        paymentTime: null,
        treasureId: null, // 充电宝Id（前端传递）
        treasureSerialNumber: null, // 充电宝序列号（前端传递）
        startTime: null, // 开始时间(后台添加)
        endTime: null,
        duration: null,
        account: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleShowMap(row) {
      this.mapOpen = true
      this.$refs.positionMap.show(row)
    },
    /** 查询充电宝列表 */
    getList() {
      this.loading = true;
      listTreasure(this.queryParams).then(response => {
        this.treasureList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        treasureId: null,
        serialNumber: null,
        availableStatus: null,
        rentalStatus: null,
        stationId: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.treasureId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加充电宝";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const treasureId = row.treasureId || this.ids
      getTreasure(treasureId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改充电宝";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.treasureId != null) {
            updateTreasure(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTreasure(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    handleRent(row) {
      this.$refs.isRentDialog.show(row)
    },
    handleShowRent(row) {
      this.$refs.rentPromt.show(row)
    },
    handleRentOk() {
      this.getList();
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const serialNumber = row.serialNumber;
      this.$modal.confirm('是否确认租借该充电宝(序列号为"' + serialNumber + '")？').then(function() {
        console.log('row:', row)
        return rent({ treasureId:row.treasureId,treasureSerialNumber:row.serialNumber });
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("租借成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/treasure/export', {
        ...this.queryParams
      }, `treasure_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
